package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.BankCardsEntity;
import io.renren.modules.app.service.BankCardsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 银行卡表
 *
 * @date 2022-04-20 22:37:45
 */
@Slf4j
@RestController
@RequestMapping("app/bankcards")
public class BankCardsController {
    @Autowired
    private BankCardsService bankCardsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:bankcards:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bankCardsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{cardId}")
    @RequiresPermissions("generator:bankcards:info")
    public R info(@PathVariable("cardId") Long cardId) {
        BankCardsEntity bankCards = bankCardsService.getById(cardId);

        return R.ok().put("bankCards", bankCards);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:bankcards:save")
    public R save(@RequestBody BankCardsEntity bankCards) {
        bankCards.setCreateTime(new Date());
        bankCardsService.save(bankCards);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:bankcards:update")
    public R update(@RequestBody BankCardsEntity bankCards) {
        bankCardsService.updateById(bankCards);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:bankcards:delete")
    public R delete(@RequestBody Long[] cardIds) {
        bankCardsService.removeByIds(Arrays.asList(cardIds));

        return R.ok();
    }


    /**
     * 获取用户银行卡信息
     *
     * @param userId 用id
     * @return
     */
    @Login
    @GetMapping("queryUserBankCard/{userId}")
    public R queryUserBankCard(@PathVariable("userId") Long userId) {
        if (userId == null) {
            return R.error("用户id不可为空");
        }
        BankCardsEntity bankCards = bankCardsService.queryUserBankCard(userId);
        return R.ok().put("bankCards", bankCards);
    }

    /**
     * 获取用户银行卡信息(过滤数据)
     *
     * @param userId 用id
     * @return
     */
    @Login
    @GetMapping("queryUserBankCardFilter/{userId}")
    public R queryUserBankCardFilter(@PathVariable("userId") Long userId) {
        if (userId == null) {
            return R.error("用户id不可为空");
        }
        BankCardsEntity bankCards = bankCardsService.queryUserBankCard(userId);
        String phone = StringUtils.encryptedPhone(bankCards.getPhone());
        bankCards.setPhone(phone);
        String cardNumber = StringUtils.hideCardNo(bankCards.getCardNumber());
        bankCards.setCardNumber(cardNumber);
        return R.ok().put("bankCards", bankCards);
    }


    /**
     * 新增or修改银行卡信息
     *
     * @param bankCardsEntity 银行卡信息类
     * @return
     */
    @Login
    @PostMapping("addOrUpdateUserBankCard")
    public R addOrUpdateUserBankCard(@RequestBody BankCardsEntity bankCardsEntity) {
        if (bankCardsEntity.getUserId() == null) {
            return R.error("用户id不可为空");
        }
        try {
            BankCardsEntity bankCards = bankCardsService.queryUserBankCard(bankCardsEntity.getUserId());
            if (bankCards == null) {
                //新增
                Integer integer = bankCardsService.addBankCards(bankCardsEntity);
                if (integer > 0) {
                    return R.ok("新增成功");
                }
            } else {
                //修改
                Integer integer = bankCardsService.editBankCards(bankCardsEntity);
                if (integer > 0) {
                    return R.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增or修改银行卡api_exception", e);
            return R.error("服务器异常，请稍后重试");
        }
        return R.error("服务器异常，请稍后重试");
    }


}
